package com.crr.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.crr.mall.common.utils.Assert;
import com.crr.mall.product.dao.AttrAttrgroupRelationDao;
import com.crr.mall.product.dao.AttrGroupDao;
import com.crr.mall.product.dao.CategoryDao;
import com.crr.mall.product.entity.AttrAttrgroupRelationEntity;
import com.crr.mall.product.entity.AttrGroupEntity;
import com.crr.mall.product.entity.CategoryEntity;
import com.crr.mall.product.vo.AttrVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.Query;

import com.crr.mall.product.dao.AttrDao;
import com.crr.mall.product.entity.AttrEntity;
import com.crr.mall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public PageUtils queryByCondition(Map map) {
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(page, pageSize);
        List<Map> list = baseMapper.queryByCondition(map);
        List<Map> list1=new ArrayList<>();
        //添加groupname和catelogname 两个属性
        list.forEach(l -> {
           Long catelog_id =(Long) l.get("catelog_id");
            Long attr_group_id =(Long) l.get("attr_group_id");
            if(null!=catelog_id){
                CategoryEntity categoryEntity = categoryDao.selectById(catelog_id);
                l.put("catelog_name",categoryEntity.getName());
            }
            if(null!=attr_group_id){
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attr_group_id);
                l.put("attr_group_name",attrGroupEntity.getAttrGroupName());
                l.put("catelog_path",attrGroupEntity.getCatelogPath());
            }
            list1.add(l);
        });
        PageInfo info = new PageInfo(list1);
        return new PageUtils(list1, (int) info.getTotal(), pageSize, page);
    }

    @Transactional
    @Override
    public int saveAttr(AttrVO attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr, attrEntity);//将传过来的attr属性copy给存在于attrEntity的属性
        int i = baseMapper.insert(attrEntity);
        Assert.fail(i == 1, "保存规格属性失败");
        //保存规格属性和分组关系表
        if(attr.getAttrType()==1&&null!=attr.getAttrGroupId()){
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(attrEntity.getAttrId());
            int insert = attrAttrgroupRelationDao.insert(relationEntity);
            Assert.fail(insert == 1, "保存规格属性分组失败");
        }
        return 1;
    }
    @Transactional
    @Override
    public int updateAttr(AttrVO attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr, attrEntity);//将传过来的attr属性copy给存在于attrEntity的属性
        int i = baseMapper.updateById(attrEntity);
        Assert.fail(i == 1, "修改规格属性失败");
        if(null!=attr.getAttrId()){
            int i1 = attrAttrgroupRelationDao.updateByAttrId(attr.getAttrId(), attr.getAttrGroupId());
           if(i1==1){
               Assert.fail(i1 == 1, "修改规格属性分组失败");
           }else {
               //原本没有则 保存规格属性和分组关系表
               AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
               relationEntity.setAttrGroupId(attr.getAttrGroupId());
               relationEntity.setAttrId(attrEntity.getAttrId());
           }

        }
        return 1;
    }


}